﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace DbHelper.Base
{
    /// <summary>
    /// 多线程同步字典
    /// </summary>
    /// <typeparam name="TKey">键类型</typeparam>
    /// <typeparam name="TValue">值类型</typeparam>
    public class SyncDictionary<TKey, TValue> : IDictionary<TKey, TValue>
    {
        ReaderWriterLockSlim dictLock = null;
        Dictionary<TKey, TValue> dictData = null;

        public SyncDictionary() : this(0) { }

        public SyncDictionary(int capacity)
        {
            dictLock = new ReaderWriterLockSlim();
            dictData = new Dictionary<TKey, TValue>(capacity);
        }

        #region IDictionary<TKey,TValue> 成员

        public void Add(TKey key, TValue value)
        {
            dictLock.EnterWriteLock();
            try
            {
                dictData.Add(key, value);
            }
            finally
            {
                dictLock.ExitWriteLock();
            }
        }

        public bool Remove(TKey key)
        {
            dictLock.EnterWriteLock();
            try
            {
                return dictData.Remove(key);
            }
            finally
            {
                dictLock.ExitWriteLock();
            }
        }

        public bool TryGetValue(TKey key, out TValue value)
        {
            dictLock.EnterReadLock();
            try
            {
                return dictData.TryGetValue(key, out value);
            }
            finally
            {
                dictLock.ExitReadLock();
            }
        }

        public TValue this[TKey key]
        {
            get
            {
                dictLock.EnterReadLock();
                try
                {
                    return dictData[key];
                }
                finally
                {
                    dictLock.ExitReadLock();
                }
            }
            set
            {
                dictLock.EnterWriteLock();
                try
                {
                    dictData[key] = value; ;
                }
                finally
                {
                    dictLock.ExitWriteLock();
                }
            }
        }

        public bool ContainsKey(TKey key)
        {
            dictLock.EnterReadLock();
            try
            {
                return dictData.ContainsKey(key);
            }
            finally
            {
                dictLock.ExitReadLock();
            }
        }

        public ICollection<TKey> Keys
        {
            get
            {
                return dictData.Keys;
            }
        }

        public ICollection<TValue> Values
        {
            get
            {
                return dictData.Values;
            }
        }

        #endregion

        #region ICollection<KeyValuePair<TKey,TValue>> 成员

        public void Add(KeyValuePair<TKey, TValue> item)
        {
            dictLock.EnterWriteLock();
            try
            {
                dictData.Add(item.Key, item.Value);
            }
            finally
            {
                dictLock.ExitWriteLock();
            }
        }

        public void Clear()
        {
            dictLock.EnterWriteLock();
            try
            {
                dictData.Clear();
            }
            finally
            {
                dictLock.ExitWriteLock();
            }
        }

        public bool Contains(KeyValuePair<TKey, TValue> item)
        {
            dictLock.EnterReadLock();
            try
            {
                return dictData.Contains(item);
            }
            finally
            {
                dictLock.ExitReadLock();
            }
        }

        public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
        {
            dictLock.EnterReadLock();
            try
            {
                ((ICollection<KeyValuePair<TKey, TValue>>)dictData).CopyTo(array, arrayIndex);
            }
            finally
            {
                dictLock.ExitReadLock();
            }
        }

        public int Count
        {
            get
            {
                int count = 0;
                dictLock.EnterReadLock();
                count = dictData.Count;
                dictLock.ExitReadLock();
                return count;
            }
        }

        public bool IsReadOnly
        {
            get
            {
                bool b = false;
                dictLock.EnterReadLock();
                b = ((ICollection<KeyValuePair<TKey, TValue>>)dictData).IsReadOnly;
                dictLock.ExitReadLock();
                return b;
            }
        }

        public bool Remove(KeyValuePair<TKey, TValue> item)
        {
            bool b = false;
            dictLock.EnterWriteLock();
            b = ((ICollection<KeyValuePair<TKey, TValue>>)dictData).Remove(item);
            dictLock.ExitWriteLock();
            return b;
        }

        #endregion

        #region IEnumerable<KeyValuePair<TKey,TValue>> 成员

        public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
        {
            return dictData.GetEnumerator();
        }

        #endregion

        #region IEnumerable 成员

        IEnumerator IEnumerable.GetEnumerator()
        {
            return dictData.GetEnumerator();
        }

        #endregion
    }
}
